module Display {
	export class TransformTest extends egret.DisplayObjectContainer{
		private static STEP_ROT:number=3;
		private static STEP_SCALE:number=.03;

		private _hero:egret.Bitmap;
		private _txInfo:egret.TextField;
		private _iAnimMode:number;
		private _nScaleBase:number;

		/**
		 * 构造函数
		 * 注册添加到舞台事件监听器
		 */
		public constructor() {
			super();
			this.addEventListener(egret.Event.ADDED_TO_STAGE,this.onAddedToStage,this);
		}

		/**
		 * 添加到舞台事件监听器
		 * @param evt:Event
		 */
		private onAddedToStage(evt:egret.Event):void{
			var imgLoader:egret.ImageLoader=new egret.ImageLoader;
			imgLoader.once(egret.Event.COMPLETE,this.imgLoaderCompleteHandler,this);
			imgLoader.load("resource/hero.png");
		}

		/**
		 * 图片加载完成监听器
		 * @param evt:Event
		 */
		private imgLoaderCompleteHandler(evt:egret.Event):void{
			//添加图片
			var bmd:egret.BitmapData=evt.currentTarget.data;
			this._hero=new egret.Bitmap(bmd);
			this.addChild(this._hero);
			this._hero.anchorOffsetX=this._hero.width/2;
			this._hero.anchorOffsetY=this._hero.height/2;
			this._hero.x=this.stage.stageWidth/2;
			this._hero.y=this.stage.stageHeight*0.6;
			//添加提示信息
			this._txInfo=new egret.TextField;
			this._txInfo.size=28;
			this._txInfo.x=150;
			this._txInfo.y=50;
			this._txInfo.textAlign=egret.HorizontalAlign.LEFT;
			this._txInfo.textColor=0x000000;
			this._txInfo.type=egret.TextFieldType.DYNAMIC;
			this._txInfo.multiline=true;
			this._txInfo.lineSpacing=6;
			this.addChildAt(this._txInfo,0);
			//启动变换动画
			this.launchAnimation();
		}		

		/**
		 * 启动变换动画
		 */
		private launchAnimation():void{
			this._iAnimMode=AnimMode.ANIM_ROT;
			this._nScaleBase=0;
			//注册触摸事件（注册到舞台上）
			this.stage.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
				this._iAnimMode=(this._iAnimMode+1)%3;
			},this);
			//注册帧事件
			this.addEventListener(egret.Event.ENTER_FRAME,(evt:egret.Event)=>{
				switch(this._iAnimMode){
					case AnimMode.ANIM_NONE:
						break;
					case AnimMode.ANIM_ROT:
						this._hero.rotation=(this._hero.rotation+TransformTest.STEP_ROT)%360;
						break;
					case AnimMode.ANIM_SCALE:
						this._hero.scaleX=this._hero.scaleY=1+0.5*Math.abs(Math.sin(this._nScaleBase+=TransformTest.STEP_SCALE));
						break;
					default:
						break;
				}
				this._txInfo.text=
				"旋转角度："+this._hero.rotation+
				"\n缩放比例："+this._hero.scaleX.toFixed(2)+
				"\n轻触进入"+(["旋转","缩放","静止"][this._iAnimMode])+"模式";
			},this);
		}
	}

	class AnimMode{
		public static ANIM_ROT:number=1;
		public static ANIM_SCALE:number=2;
		public static ANIM_NONE:number=0;
	}
}